<template>
  <div>
    <el-card class="goods-list" shadow="hover">
    <div class="action-btns">
      <el-input v-model="searchText" placeholder="请输入搜索关键字" class="search-input"></el-input>
      <el-button type="primary" @click="handleAdd" class="newBTN">新增供应商</el-button>
    </div>
    <el-table :data="filteredSuppliers" style="width: 100%" border>
      <el-table-column prop="id" label="供应商ID" width="100"></el-table-column>
      <el-table-column prop="name" label="供应商名称" width="200"></el-table-column>
      <el-table-column prop="phone" label="联系电话" width="200"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="200"></el-table-column>
      <el-table-column prop="introduce" label="介绍"></el-table-column>
      <el-table-column label="操作" width="100" fixed="right" >
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogType == 'add' ? '新增' : '编辑'" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-form :model="formData" ref="supplierForm" label-width="80px">
          <el-form-item label="供应商" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="formData.phone"></el-input>
          </el-form-item>
          <el-form-item label="介绍" prop="introduce">
            <el-input type="textarea" v-model="formData.introduce"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { querySupplier, insertSupplier, updateSupplier, querySupplierDetail } from '../../../api/index';

export default {
  data() {
    return {
      searchText: '',
      suppliers: [],
      dialogVisible: false, // 控制对话框显示/隐藏
      dialogType: '' ,// 对话框类型，'add' 表示新增，'edit' 表示编辑
      formData: {
        id: '',
        name: '',
        phone: '',
        introduce: ''
      }
    };
  },
  mounted() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      querySupplier().then(res => {
        this.suppliers = res.data;
      });
    },

    handleAdd() {
      this.dialogType = 'add'; // 设置对话框类型为新增
      for (let key in this.formData) {
        this.formData[key] = null;
      }
      this.dialogVisible = true; // 打开对话框
    },
    handleEdit(row) {
      this.dialogType = 'edit'; // 设置对话框类型为编辑
      this.dialogVisible = true; // 打开对话框
      this.formData = { ...row }; // 将行数据填充到表单数据对象中
    },
    handleSave() {
      // 处理保存操作，根据对话框类型执行不同的操作
      if (this.dialogType === 'add') {
        insertSupplier(this.formData).then(res=>{
          this.$message.success("新增供应商成功！")
          this.getInfo();
        })
      } else if (this.dialogType === 'edit') {
        // 执行编辑操作
        updateSupplier(this.formData).then(res=>{
          this.$message.success("编辑供应商成功！")
          this.getInfo();
        })
      }

      this.dialogVisible = false; // 关闭对话框
    },

  },
  computed: {
    filteredSuppliers() {
      if (!this.searchText) {
        return this.suppliers;
      }
      const searchTextLower = this.searchText.toLowerCase();
      return this.suppliers.filter(supplier => {
        return Object.values(supplier).some(value => {
          if (typeof value === 'string') {
            return value.toLowerCase().includes(searchTextLower);
          }
          return false;
        });
      });
    }
  }
};
</script>

<style scoped>
.search-input {
  margin-bottom: 10px;
}
.action-btns {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}

.newBTN {
  margin-left: 10px;
  height: 40px;
}
</style>
